Trò chơi đua xe động vật trong UNITY Engine
- CurvedLayout.cs
- Layout /
- AnimalRacing /
- Scripts /
- Assets /
- project /
2 /// Curved Layout Group Created by Freezy - http://www.ElicitIce.com
3 /// Posted on Unity Forums http://forum.unity3d.com/threads/curved-layout.403985/
4 ///
5 /// Free for any use and alteration, source code may not be sold without my permission.
6 /// If you make improvements on this script please share them with the community.
7 ///
8 /// </summary>
9
10 namespace UnityEngine.UI.Extensions {
11 /// <summary>
12 /// TODO:
13 /// - add automatic child sizing, like in the HorizontalOrVerticalLayoutGroup.cs
14 /// - nicer anchor handling for initial child positions
15 /// </summary>
16 [AddComponentMenu("Layout/Extensions/Curved Layout")]
17 public class CurvedLayout : LayoutGroup {
18 public Vector3 CurveOffset;
19
20 // Yes these two could be combined into a single vector
21 // but this makes it easier to use?
22 [Tooltip("axis along which to place the items, Normalized before use")]
23 public Vector3 itemAxis;
24 [Tooltip("size of each item along the Normalized axis")]
25 public float itemSize;
26
27 // the slope can be moved by altering this setting, it could be constrained to the 0-1 range, but other values are usefull for animations
28 public float centerpoint = 0.5f;
29
30 protected override void OnEnable() { base.OnEnable(); CalculateRadial(); }
31 public override void SetLayoutHorizontal() {
32 }
33 public override void SetLayoutVertical() {
34 }
35 public override void CalculateLayoutInputVertical() {
36 CalculateRadial();
37 }
38 public override void CalculateLayoutInputHorizontal() {
39 CalculateRadial();
40 }
41 #if UNITY_EDITOR
42 protected override void OnValidate() {
43 base.OnValidate();
44 CalculateRadial();
45 }
46 #endif
47
48 void CalculateRadial() {
49 m_Tracker.Clear();
50 if (transform.childCount == 0)
51 return;
52
53 //one liner for figuring out the desired pivot (should be moved into a utility function)
54 Vector2 pivot = new Vector2(((int)childAlignment % 3) * 0.5f, ((int)childAlignment / 3) * 0.5f);
55
56 //this seems to work ok-ish
57 Vector3 lastPos = new Vector3(
58 GetStartOffset(0, GetTotalPreferredSize(0)),
59 GetStartOffset(1, GetTotalPreferredSize(1)),
60 0f
61 );
62
63 // 0 = first, 1 = last child
64 float lerp = 0;
65 //no need to catch divide by 0 as childCount > 0
66 float step = 1f / transform.childCount;
67
68 //normalize and create a distance between items
69 var dist = itemAxis.normalized * itemSize;
70
71 for (int i = 0; i < transform.childCount; i++) {
72 RectTransform child = (RectTransform)transform.GetChild(i);
73 if (child != null) {
74 //stop the user from altering certain values in the editor
75 m_Tracker.Add(this, child,
76 DrivenTransformProperties.Anchors |
77 DrivenTransformProperties.AnchoredPosition |
78 DrivenTransformProperties.Pivot);
79 Vector3 vPos = lastPos + dist;
80
81 child.localPosition = lastPos = vPos + (lerp - centerpoint) * CurveOffset;
82
83 child.pivot = pivot;
84 //child anchors are not yet calculated, each child should set it's own size for now
85 child.anchorMin = child.anchorMax = new Vector2(0.5f, 0.5f);
86 lerp += step;
87 }
88 }
89
90 }
91 }
92 }
Curved Layout Group Created by Freezy - http:www.ElicitIce.com
Posted on Unity Forums http:forum.unity3d.comthreadscurved-layout.403985
Free for any use and alteration, source code may not be sold without my permission.
If you make improvements on this script please share them with the community.
TODO:
- add automatic child sizing, like in the HorizontalOrVerticalLayoutGroup.cs
- nicer anchor handling for initial child positions
Yes these two could be combined into a single vector
but this makes it easier to use?
the slope can be moved by altering this setting, it could be constrained to the 0-1 range, but other values are usefull for animations
one liner for figuring out the desired pivot (should be moved into a utility function)
this seems to work ok-ish
0 = first, 1 = last child
no need to catch divide by 0 as childCount > 0
normalize and create a distance between items
stop the user from altering certain values in the editor
child anchors are not yet calculated, each child should set it's own size for now